package pres.liko.iot.gateway.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pres.liko.iot.gateway.events.BlackApiChangeEvent;
import pres.liko.iot.gateway.events.BlackIpChangeEvent;

import java.util.function.Consumer;

/**
 * @ClassName: RabbitmqListenerCOnfig
 * @Auther: liko
 * @Description:
 * @Date: 2022/11/2 11:48
 */
@Configuration
public class RabbitmqListenerConfig {
    private ApplicationContext applicationContext;

    @Autowired
    public RabbitmqListenerConfig(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * 消费者接收 ip接收
     *
     * @return
     */
    @Bean
    public Consumer<String> onMessage() {
        return s -> {
            System.out.println("网关收到 BlackIpChange");
            applicationContext.publishEvent(new BlackIpChangeEvent());
        };
    }

    /**
     * 消费者接收 api
     *
     * @return
     */
    @Bean
    public Consumer<String> onMessageApi() {
        return s -> {
            System.out.println("网关收到 BlackApiChange");
            applicationContext.publishEvent(new BlackApiChangeEvent());
        };
    }
}
